package com.lazily.set_;


import java.util.LinkedHashSet;
import java.util.Objects;


/**
 * @Author:  GZQ
 * @date:  2025/5/27 23:06
 * @description: LinkedHashSet 练习题
 * @version: 1.0
 */

@SuppressWarnings({"all"})
public class LinkedHashSetExercise {
	// 程序入口
	public static void main(String[] args) {
	/*
		Car 类(属性：name, price), 如果 name 和 price 一样, 则认为是相同元素, 就不能添加。
	 */
		LinkedHashSet linkedHashSet = new LinkedHashSet();
		linkedHashSet.add(new Car("奥拓", 1000));//OK
		linkedHashSet.add(new Car("奥迪", 300000));//OK
		linkedHashSet.add(new Car("法拉利", 10000000));//OK
		linkedHashSet.add(new Car("奥迪", 300000));//加入不了
		linkedHashSet.add(new Car("保时捷", 70000000));//OK
		linkedHashSet.add(new Car("奥迪", 300000));//加入不了

		System.out.println("汽车信息：" + linkedHashSet);
	}
}

@SuppressWarnings({"all"})
class Car {
	private String name;
	private  double price;

	public Car(String name, double price) {
		this.name = name;
		this.price = price;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "\n" + "车名: " +  name + "\t" + "价格: " + price;
	}

	//重写 equals 方法 和 hashCode
	//当 name 和 price 相同时， 就返回相同的 hashCode 值, equals 返回 true

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Car car = (Car)o;
		return Double.compare(price, car.price) == 0 && Objects.equals(name, car.name);
	}

	@Override
	public int hashCode() {
		return Objects.hash(name, price);
	}
}